//
//  MainMenuScreen.m
//  TowerDefence
//
//  Created by K3 on 5/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainMenuScreen.h"
#import "iCoreGUIController.h"

@implementation MainMenuScreen

-(id)initWith:(iCoreGUIController *)_mainGUIController {
    self = [super initGameLayerWith:_mainGUIController]; 
    
    [self loadResource];
    //start menu screen animation
    [self startAnimation];

    return self;
}

-(void)loadResource {       
    //Play sound
    [mainGUIController playBackgroundSound:AUDIO_MAIN_MENU_BACKGROUND withLoop:YES];
    [mainGUIController setBackgroundSoundVolume:0.05f];
    [mainGUIController preloadSoundEffectFile:AUDIO_BUTTON_CLICKED];
    
    //load graphic
    [mainGUIController loadGraphicFromPlistFileToFrameCache:GRAPHIC_MAIN_MENU_PLIST_FILE];
    [mainGUIController loadGraphicFromPlistFileToFrameCache:GRAPHIC_OTHER_PLIST_FILE];
    
    CGSize windowsSize = [mainGUIController.mainDirector winSize];
    
    backgroundSprite = [CCSprite spriteWithFile:IMAGE_MAIN_MENU_BACKGROUND];
    backgroundSprite.position = ccp(0, 0);
    backgroundSprite.anchorPoint = ccp(0, 0);
    [self addChild:backgroundSprite z:0];
    
    //Draw button Game center
    CCMenuItemSprite *menuItemGameCenter = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"icon_game_center.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"icon_game_center.png"] target:self selector:@selector(connectGameCenter)];
    [menuItemGameCenter runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCRotateTo actionWithDuration:0.15f angle:2.5f], [CCRotateTo actionWithDuration:0.15f angle:-2.5f], nil]]];
    CCLabelTTF *gameCenterLabel = [CCLabelTTF labelWithString:@"Game Center" fontName:FONT_COMIC_BOOK fontSize:12];
    gameCenterLabel.color = ccc3(255, 255, 64);
    gameCenterLabel.position = ccp(menuItemGameCenter.contentSize.width/2, -5);
    [menuItemGameCenter addChild:gameCenterLabel];
    
    menuButtonGameCenter = [CCMenu menuWithItems:menuItemGameCenter, nil];
    menuButtonGameCenter.anchorPoint = ccp(1, 1);
    menuButtonGameCenter.position = ccp(windowsSize.width - menuItemGameCenter.contentSize.width/2 - 10, windowsSize.height - menuItemGameCenter.contentSize.height/2 - 10);
    [self addChild:menuButtonGameCenter];
    
    gameNameImage = [CCSprite spriteWithSpriteFrameName:@"gameNameImage.png"];
    gameNameImage.position = ccp(windowsSize.width/2, windowsSize.height - gameNameImage.contentSize.height/2);
    gameNameImage.anchorPoint = ccp(0.5f, 0.5f);
    //start hide this sprite
    gameNameImage.scale = 0;
    [self addChild:gameNameImage z:1];
    
    playChainMenuItem = [CCSprite spriteWithSpriteFrameName:@"menuChainItem.png"];
    playChainMenuItem.scale = 0.8;
    playChainMenuItem.visible = NO;
    playChainMenuItem.anchorPoint = ccp(0.5f, 1.0f);
    playChainMenuItem.position = ccp(windowsSize.width/2, gameNameImage.position.y + playChainMenuItem.contentSize.height/2 - 30);
    [self addChild:playChainMenuItem z:0];
    
    CCMenuItemSprite *playButton = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"startButtonNormal.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"startButtonSelected.png"] target:self selector:@selector(startPress:)];
    playButton.scale = 0.7f;
    playButton.anchorPoint = ccp(0, 0);
    
    myMenu = [CCMenu menuWithItems:playButton, nil];
    myMenu.position = ccp(2, 0);
    [playChainMenuItem addChild:myMenu];
    
    //create and add menutable Sprite
    menuTable = [CCSprite spriteWithSpriteFrameName:@"menuTable.png"];
    menuTable.position = ccp(windowsSize.width/2, menuTable.contentSize.height/2 + 20);
    menuTable.scale = 0;
    menuTable.visible = NO;
    [self addChild:menuTable z:0];
    
    CCLabelTTF *newGameTitle = [CCLabelTTF labelWithString:@"Play Game" fontName:FONT_COMIC_BOOK fontSize:12];
    newGameTitle.color = ccc3(255, 255, 64);
    newGameTitle.position = ccp(menuTable.contentSize.width - 60, menuTable.contentSize.height - 15);
    [menuTable addChild:newGameTitle];
    
    //create sprite normal button and selected button
    CCSprite *normalButtonSprite = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];        
    CCLabelTTF *labelNormal = [CCLabelTTF labelWithString:@"PLAY GAME" fontName:FONT_COMIC_BOOK fontSize:12];
    labelNormal.color = ccc3(255, 254, 196);
    labelNormal.position = ccp(normalButtonSprite.contentSize.width/2, normalButtonSprite.contentSize.height/2);
    [normalButtonSprite addChild:labelNormal];
    
    CCSprite *selectedButtonSprite = [CCSprite spriteWithSpriteFrameName:@"buttonSelected.png"];
    CCLabelTTF *labelSelected = [CCLabelTTF labelWithString:@"PLAY GAME" fontName:FONT_COMIC_BOOK fontSize:12];
    labelSelected.color = ccc3(255, 255, 74);
    labelSelected.position = ccp(selectedButtonSprite.contentSize.width/2, selectedButtonSprite.contentSize.height/2);
    [selectedButtonSprite addChild:labelSelected];
    
    CCMenuItemSprite *newGameButton = [CCMenuItemSprite itemFromNormalSprite:normalButtonSprite selectedSprite:selectedButtonSprite target:self selector:@selector(startNewGame)];
    
    //create sprite normal button and selected button
    CCSprite *normalCreditButtonSprite = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];        
    CCLabelTTF *labelCreditNormal = [CCLabelTTF labelWithString:@"CREDIT" fontName:FONT_COMIC_BOOK fontSize:12];
    labelCreditNormal.color = ccc3(255, 254, 196);
    labelCreditNormal.position = ccp(normalButtonSprite.contentSize.width/2, normalButtonSprite.contentSize.height/2);
    [normalCreditButtonSprite addChild:labelCreditNormal];
    
    CCSprite *selecteCreditdButtonSprite = [CCSprite spriteWithSpriteFrameName:@"buttonSelected.png"];
    CCLabelTTF *labelCreditSelected = [CCLabelTTF labelWithString:@"CREDIT" fontName:FONT_COMIC_BOOK fontSize:12];
    labelCreditSelected.color = ccc3(255, 255, 74);
    labelCreditSelected.position = ccp(selectedButtonSprite.contentSize.width/2, selectedButtonSprite.contentSize.height/2);
    [selecteCreditdButtonSprite addChild:labelCreditSelected];
    
    CCMenuItemSprite *creditButton = [CCMenuItemSprite itemFromNormalSprite:normalCreditButtonSprite selectedSprite:selecteCreditdButtonSprite target:self selector:@selector(viewCredit)];
    
    newGameMenu = [CCMenu menuWithItems:newGameButton, creditButton, nil];
    newGameMenu.position = ccp(menuTable.contentSize.width - 63, menuTable.contentSize.height - 80);
    [newGameMenu alignItemsVerticallyWithPadding:10.0f];
    [menuTable addChild:newGameMenu z:1 tag:TAG_NEW_GAME_MENU_ITEM];
    
    //add mute speaker menu icon
    //create 2 menu item image, this show speaker normal image fist
    CCMenuItemSprite *speakerControl = [CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"speakerNormal.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"speakerMute.png"] target:nil selector:nil];
    
    //this menuItemImage show speaker mute image fist
    CCMenuItemImage *speakerControlReverse = [CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"speakerMute.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"speakerNormal.png"] target:nil selector:nil];
    
    //Create toogle to switch between speakerControl and speakerControlReverse
    CCMenuItemToggle *speakerMenuItem = nil;
    if(mainGUIController.isMuteSound)
        speakerMenuItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(muteAllSound:) items:speakerControlReverse, speakerControl, nil];
    else
        speakerMenuItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(muteAllSound:) items:speakerControl, speakerControlReverse, nil];
    speakerMenuItem.scale = 1.25f;
    speakerMenu = [CCMenu menuWithItems:speakerMenuItem, nil];
    speakerMenu.position = ccp(25, 300);
    [self addChild:speakerMenu z:0 tag:TAG_SPEAKER_MENU];
    
    //draw user information
    [self drawPlayerInfo];
}

-(void)drawPlayerInfo {    
    playerTitle = [CCLabelTTF labelWithString:@"Player" fontName:FONT_COMIC_BOOK fontSize:12];
    playerTitle.color = ccc3(255, 255, 64);
    playerTitle.position = ccp(60, menuTable.contentSize.height - 15);
    [menuTable addChild:playerTitle];
    
    /*
     CCSprite *moneyIcon = [CCSprite spriteWithFile:IMAGE_PLAYER_INFO_MONEY];
     moneyIcon.anchorPoint = ccp(1, 1);
     moneyIcon.position = ccp(50, menuTable.contentSize.height - 30);
     [menuTable addChild:moneyIcon];
     
     CCLabelTTF *moneyCountText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", mainGUIController.playerData.money] fontName:FONT_COMIC_BOOK fontSize:18];
     moneyCountText.anchorPoint = ccp(0, 1);
     moneyCountText.position = ccp(70, menuTable.contentSize.height - 40);
     moneyCountText.color = ccc3(255, 255, 64);
     [menuTable addChild:moneyCountText]; */
    playerData = [[PlayerData alloc] initUserDataWith:mainGUIController];
    NSString *username = playerData.playerName;

    playerNameLabel = [CCLabelTTF labelWithString:username fontName:FONT_COMIC_BOOK fontSize:18];
    playerNameLabel.anchorPoint = ccp(0, 1);
    playerNameLabel.position = ccp(50, menuTable.contentSize.height - 30);
    playerNameLabel.color = ccc3(255, 255, 64);
    [menuTable addChild:playerNameLabel];
    
    
    playerName = [CCLabelTTF labelWithString:mainGUIController.playerData.playerName fontName:FONT_COMIC_BOOK fontSize:18];
    playerName.anchorPoint = ccp(0, 1);
    playerName.position = ccp(70, menuTable.contentSize.height - 50);
    playerName.color = ccc3(255, 255, 64);
    [menuTable addChild:playerName];
    
    starIcon = [CCSprite spriteWithFile:IMAGE_PLAYER_INFO_STAR];
    starIcon.anchorPoint = ccp(1, 1);
    starIcon.position = ccp(50, menuTable.contentSize.height - 70);
    [menuTable addChild:starIcon];
    
    starCountText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", mainGUIController.playerData.star] fontName:FONT_COMIC_BOOK fontSize:18];
    starCountText.anchorPoint = ccp(0, 1);
    starCountText.position = ccp(70, menuTable.contentSize.height - 80);
    starCountText.color = ccc3(255, 255, 64);
    [menuTable addChild:starCountText];
    
    lifeIcon = [CCSprite spriteWithFile:IMAGE_PLAYER_INFO_LIFE];
    lifeIcon.anchorPoint = ccp(1, 1);
    lifeIcon.position = ccp(50, menuTable.contentSize.height - 110);
    [menuTable addChild:lifeIcon];
    
    lifeCountText = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", mainGUIController.playerData.life] fontName:FONT_COMIC_BOOK fontSize:16];
    lifeCountText.anchorPoint = ccp(0, 1);
    lifeCountText.position = ccp(70, menuTable.contentSize.height - 120);
    lifeCountText.color = ccc3(255, 255, 64);
    [menuTable addChild:lifeCountText];
}

-(void)startAnimation {        
    CCCallFunc *callStartChainMenuItemAnimationFunc = [CCCallFunc actionWithTarget:self selector:@selector(startChainMenuItemAnimation)];
    
    //animation gamenameImage scale from 0 to 1
    [gameNameImage runAction: [CCSequence actions:[CCScaleTo actionWithDuration:0.5f scale:1.15f], [CCScaleTo actionWithDuration:0.15 scale:0.8f], [CCScaleTo actionWithDuration:0.25 scale:1.0f], callStartChainMenuItemAnimationFunc ,nil]];
}

-(void)startChainMenuItemAnimation {    
    playChainMenuItem.visible = YES;
    
    CGPoint stopPos = ccp(0, - playChainMenuItem.contentSize.height/1.4);
    [playChainMenuItem runAction:[CCSequence actions:
                                  [CCMoveBy actionWithDuration:0.55f position:ccp(0, stopPos.y - 5)],
                                  [CCMoveBy actionWithDuration:0.25 position:ccp(0, + 10)],
                                  [CCMoveBy actionWithDuration:0.15 position:ccp(0, - 5)],
                                  nil]];
}

-(void)startPress:(CCMenuItemImage *)sender {
    //play button sound 
    [mainGUIController playEffect:AUDIO_BUTTON_CLICKED];
    
    CCCallFunc *callShowMenuTable = [CCCallFunc actionWithTarget:self selector:@selector(showMenuTable)];
    
    //slideUp IMAGE_MAIN_MENU_ITEM_CHAIN
    CGPoint stopPos = ccp(0, playChainMenuItem.contentSize.height/1.4);
    [playChainMenuItem runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.5f position:stopPos], [CCHide action], callShowMenuTable, nil]];
}

-(void)muteAllSound:(id)sender {
    [mainGUIController setMuteSoundPlayer:!mainGUIController.isMuteSound]; 
}

-(void)showMenuTable {
    menuTable.visible = YES;
    
    [menuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.3 scale:1.2f], [CCScaleTo actionWithDuration:0.15 scale:0.8], [CCScaleTo actionWithDuration:0.1 scale:1.0f], nil]];
}

-(void)startNewGame {
    //play button sound 
    [mainGUIController playEffect:AUDIO_BUTTON_CLICKED];
    [mainGUIController runGameScreen:GAME_SCREEN_SELECT_MAP];
}

-(void)viewCredit {
    NSLog(@"view credit");
    [mainGUIController playEffect:AUDIO_BUTTON_CLICKED];
}

-(void)connectGameCenter {
    //[mainGUIController connectGameCenter];
    //MapInfoPopup *map = [[MapInfoPopup alloc]initMapInfoPopupWith:self mapDataFileName:@""];
    //[map showPopup];
    [mainGUIController playEffect:AUDIO_BUTTON_CLICKED];
    CharacterInfoPopup *popup = [[CharacterInfoPopup alloc]initCharacterInfoPopupWith:self];
    [popup showPopup];
}

-(void)dealloc {
    NSLog(@"main menu dealloc");
    [mainGUIController unloadSoundEffectFile:AUDIO_BUTTON_CLICKED];
    
    [playerTitle removeFromParentAndCleanup:YES];
    [playerNameLabel removeFromParentAndCleanup:YES];
    [playerName removeFromParentAndCleanup:YES];
    [starIcon removeFromParentAndCleanup:YES];
    [starCountText removeFromParentAndCleanup:YES];
    [lifeIcon removeFromParentAndCleanup:YES];
    [lifeCountText removeFromParentAndCleanup:YES];
    [menuTable removeFromParentAndCleanup:YES];
    [backgroundSprite removeFromParentAndCleanup:YES];
    [playChainMenuItem removeFromParentAndCleanup:YES];
    [gameNameImage removeFromParentAndCleanup:YES];
    
    [menuButtonGameCenter removeFromParentAndCleanup:YES];
    //[myMenu removeFromParentAndCleanup:YES];
    //[newGameMenu removeFromParentAndCleanup:YES];
    [speakerMenu removeFromParentAndCleanup:YES];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    
    [mainGUIController cleanupFrameCacheSprite];
    
    [super dealloc];
}

@end
